/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var $store = {
    host:"http://localhost/store/application",
    language:'en'
};

var PROJECT = 'tuts';
var APP_FOLDER = 'application';
var BASE_URL = '';
if(location.protocal){
    BASE_URL = location.protocal + location.hostname + '/' + PROJECT + '/';
}
else{
    BASE_URL = 'http://' + location.hostname + '/' + PROJECT + '/';
}
var SITE_URL = BASE_URL + 'index.php';
var APP_URL = BASE_URL + APP_FOLDER;

var $lang = {
    phrases: '',
    init: function(){
        this.phrases = this.get();
    },
    get: function(){
        var $txt = $('#phrase_js').html();
        ///////////////////////////////
        $txt = $txt.replace(/^<\!\-\-|(\-\->)$/, '');
        $txt = $txt.replace("-->", '');
        return JSON.parse($txt);
    }
};

var $loader = {
    id: 'loading_mask',
    name: 'loading_mask',
    start: function(){
        $('#' + this.id).show();
    },
    stop: function(){
        $('#' + this.id).hide();
    },
    complete: function(){
        $('#' + this.id).hide();
    }
};

var $page = {
    action: '',
    class: 'page',
    container: '',
    content: '',
    module:'',
    objModule: null,
    init: function($moduleName, $objModule) {
        this.module = $moduleName;
        this.objModule = $objModule;
        this.get(0)
                ._intEvent();
        this.container = $('#pagination');
    },
    _intEvent: function() {
        this._initEventChangePage()
                ._initEventSetView();
    },
    _initEventChangePage: function() {
        $('.' + this.class).live('click', function($event) {
            $event.preventDefault();
            //Show loader icon.
            $loader.start();
            this.action = $(this).attr('href');

            var $option = {
                type: 'post',
                url: this.action,
                dataType: 'json',
                data: $page.objModule.getSearchObject(),
                success: function($response, $status) {
                    if ($response.result) {
                        $page.create($response.data.pages)
                                .addClass(this.class);

                        $page.objModule.createList($response.data.list);
                        //Hide loader icon.
                        $loader.complete();
                    }
                }
            };

            $project.ajax($option);

        });

        return this;
    },
    _initEventSetView: function() {
        //Choose number of element to view..
        $('#pagination_number_view').live('change', (function($event) {
            $loader.start();
            var $num_view = $(this).val();
            var $option = {
                type: 'post',
                url: $project.siteUrl($page.module + '/admincp/index/ajax_set_view'),
                dataType: 'json',
                data: {num_view: $num_view},
                success: function($response, $status) {
                    if ($response.result) {
                        $page.get(0);
                        //Hide loader icon.
                        $loader.complete();
                    }
                }
            };

            $project.ajax($option);

        }));

        return this;
    },
    get: function($pageNum) {
        //Show loader icon.
        $loader.start();

        this.action = $project.siteUrl($page.module + '/admincp/index/ajax_get_list');

        if ($pageNum) {
            this.action += '/' + $pageNum;
        }
        else {
            this.action += '/' + 1;
        }

        var $option = {
            type: 'post',
            url: this.action,
            dataType: 'json',
            data: $page.objModule.getSearchObject(),
            success: function($response, $status) {
                if ($response.result) {
                    $page.create($response.data.pages)
                            .addClass(this.class);
                    $page.objModule.createList($response.data.list);
                    //Hide loader icon.
                    $loader.complete();
                }
            }
        };

        $project.ajax($option);


        return this;
    },
    create: function($data) {
//        this.content = JSON.parse($data);
        this.content = $data;
        this.container.empty().append(this.content);

        return this;
    },
    addClass: function() {
        this.container.find('a').addClass(this.class);

        return this;
    }
};